<?php
namespace Admin\Model;

use Common\Model\CommonModel;

class CardModel extends CommonModel {

    /**
     * 获取卡片列表
     */
    function get_list($where = array(),$order = '',$from =  0,$to = 0){
        $list = M("Card")->alias("c")->field("c.*,l.logistics_name,ca.car_master,ca.car_mobile,ca.create_time AS register_time,ca.car_code,d.phone AS driver_phone,d.driver_name,u.user_nicename")->where($where)
            ->join("bdt_logistics l ON l.id = c.lid","LEFT")
            ->join("bdt_car ca ON ca.card_id = c.id","LEFT")
            ->join("bdt_driver d ON d.id = ca.driver_id","LEFT")
            ->join("bdt_users u ON ca.uid = u.id","LEFT")
            ->order($order)
            ->limit($from , $to)
            ->select();
        return $list;
    }

    /**
     * 根据会员卡号获取会员卡信息
     */
    public function get_one($where){
        $card = M("Card")->alias("c")->field("c.*,l.logistics_name,d.driver_name,ca.car_code")->where($where)
            ->join("bdt_logistics l ON l.id = c.lid","LEFT")
            ->join("bdt_car ca ON ca.card_id = c.id","LEFT")
            ->join("bdt_driver d ON d.id = ca.driver_id","LEFT")
            ->find();
        return $card;
    }

    /**
     * 批量充值 获取所有金额
     */
    public function get_all_recharge_price($list){
        $admin_card_active_price = get_config_by_key('admin_card_active_price');    //管理员开卡费
        $logistics_card_give_price = get_config_by_key('logistics_card_give_price');    //管理员开发费
        $price = 0;
        foreach($list AS $ritem){
            $card = $this->get_one(array("c.number"=>$ritem['card_number']));
            $price = $price+$ritem['price']+$logistics_card_give_price;
            if($card['is_active'] != 1){    //已激活
                $price = $price+$admin_card_active_price;
            }
        }
        return $price;
    }

    /**
     * 充值会员卡
     */
    public function recharge_price($card_number,$price,$desc){
        $logistics =get_logistics_data();
        $card = $this->get_one(array("c.number"=>$card_number));
        $admin_card_give_price = get_config_by_key('admin_card_give_price');    //管理员充值赠送金额
        $admin_card_active_price = get_config_by_key('admin_card_active_price');    //管理员开卡费
        $logistics_card_give_price = get_config_by_key('logistics_card_give_price');    //管理员开发费
        $recharge_price = $price+$logistics_card_give_price;
        if($card['is_active'] != 1){
           $recharge_price = $recharge_price+$admin_card_active_price;
        }
        if($recharge_price>$logistics['balance']){ //金额不足
            return -2;
        }
        $insert = array(
            'rid'=>$card['id'],
            'price'=>$price,
            'desc'=>$desc,
            'general_price'=>$admin_card_give_price,
            'self_price'=>$logistics_card_give_price,
        );
        if($card['recharge_num'] == 0){
            $insert['active_price'] = $admin_card_active_price;
        }
        $result = D("Admin/Recharge")->insert_data($insert,2);
        if($result){    //充值成功 更新充值次数
            $update =array("recharge_num"=>$card['recharge_num']+1);
            if($card['recharge_num'] == 0){
                $update['is_active'] = 1;
                $update['active_price'] = $admin_card_active_price;
                M("Logistics")->where(array("id"=>$logistics['id']))->save(array('active_card_num'=>$logistics['active_card_num']+1));
            }
            $this->where(array("id"=>$card['id']))->save($update);

        }
        return $result;
    }

}